package org.example.myerp.service.serviceimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.nacos.client.utils.StringUtils;
import org.example.myerp.mapper.MenuMapper;
import org.example.myerp.pojo.*;
import org.example.myerp.service.MenuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Override
    public PageMyself<Menu> findByPager(Integer pageNum, Integer pageSize) {
        Map params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        PageMyself<Menu> page = new PageMyself<Menu>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        List<Menu> lis = menuMapper.findByPager(params);
        page.setRows(lis);
        page.setTotal(menuMapper.pageTotal());
        return page;
    }

    @Override
    public List<Menu> oneLevelIdName() {
        return menuMapper.oneLevelIdName();
    }

    @Override
    public Integer addMenu(Menu menu) {
        return menuMapper.addMenu(menu);
    }

    @Override
    public void delMenus(Integer mid) {
        menuMapper.delMenus(mid);
    }

    @Override
    public void batchDdelMenus(int[] mids) {
        menuMapper.batchDdelMenus(mids);

    }

    @Override
    public void updateMenu(Menu menu) {
        menuMapper.updateMenu(menu);
    }

    @Override
    public List<LoadMenus> loadMainMenu(Integer uid) {
        List<LoadMenus> LoadMenus = new ArrayList<LoadMenus>();
        List<Menu> loadMainMenu = menuMapper.loadMainMenu(uid);
        for (Menu menu : loadMainMenu) {
            if (Integer.valueOf(menu.getP_mid()) == 0) {
                LoadMenus load = new LoadMenus();
                load.setMid(String.valueOf(menu.getMid()));
                load.setMname(menu.getMname());
                load.setPath(menu.getPath());
                if (Integer.valueOf(menu.getP_mid()) == 0) {
                    List<LoadMenus> notes = new ArrayList<LoadMenus>();
                    for (Menu menu2 : loadMainMenu) {
                        if (Integer.valueOf(load.getMid()) == menu2.getP_mid()) {
                            LoadMenus note = new LoadMenus();
                            note.setMid(String.valueOf(menu2.getMid()));
                            note.setMname(menu2.getMname());
                            note.setP_mid(Integer.valueOf(menu2.getP_mid()));
                            note.setPath(menu2.getPath());
                            notes.add(note);
                        }
                    }
                    load.setChildren(notes);
                }
                LoadMenus.add(load);
            }

        }
        return LoadMenus;
    }

    @Override
    public WebRoleTree roleTree(Integer rid) {
        List<Integer> lis = menuMapper.roleMenus(rid);
        WebRoleTree webRoleTree = new WebRoleTree(lis, this.getMenus());
        return webRoleTree;
    }


    public List<Tree> getMenus() {
        List<Menu> menu = menuMapper.allMenus();
        System.out.println(menu);
        // ׼������
        List<Tree> menuList = new ArrayList<Tree>();

        // ���������˵�����Ӷ�ݹ鷽��
        for (Menu m : menu) {
            if (  m.getP_mid() == 0 ) {
                Tree t = new Tree();
                t.setMid(m.getMid());
                t.setLabel(m.getMname());
                t.setPid(m.getP_mid());
                t.setHerf(m.getPath());
                menuList.add(t);
            }
        }
        // ���������˵�����Ӷ�ݹ鷽��
        for (Tree children : menuList) {
            // ��Ӷ�ݹ鷽��
            //children.setChildren(getChild(children.getMid(), menu));
            //
            children.setChildren(iterateMenus(menu, children.getMid()));
        }
        return menuList;
    }

    private List<Tree> getChild(Integer id, List<Menu> rootMenu) {
        // ׼�������Ӳ˵�
        List<Tree> childList = new ArrayList<Tree>();
        // �������в˵�
        for (Menu m : rootMenu) {
            Tree t = new Tree();
            t.setMid(m.getMid());
            t.setLabel(m.getMname());
            t.setPid(m.getP_mid());
            t.setHerf(m.getPath());
            if (m.getP_mid() != 0 && m.getP_mid() == id) {
                childList.add(t);
            }
        }
        // ��childList ����Ӳ˵���ѭ��һ��
        for (Tree tree : childList) {
            if (tree.getHerf() == null || tree.getHerf().equals("")) {
                // �ݹ�(�ٴε��÷���, �Ըò˵�idΪ����.�ٴκ����в˵����б����Ƚ�.�ҳ������Ӳ˵�)
                tree.setChildren(getChild(tree.getMid(), rootMenu));
            }
        }
        // �ݹ��˳�����: ���childList.size() == 0,��������ݽ����Ĳ˵���û���Ӳ˵�.
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    /**
     * 多级菜单查询方法
     *
     * @param menuVoList 不包含最高层次菜单的菜单集合
     * @param pid        父类id
     * @return
     */
    public List<Tree> iterateMenus(List<Menu> menuVoList, Integer pid) {
        List<Tree> result = new ArrayList<Tree>();
        for (Menu menu : menuVoList) {
            //获取菜单的id
            Integer menuid = menu.getMid();
            //获取菜单的父id
            Integer parentid = menu.getP_mid();

            if (StringUtils.isNotBlank(String.valueOf(parentid))) {
                if (parentid.equals(pid)) {
                    //递归查询当前子菜单的子菜单

                    List<Tree> iterateMenu = iterateMenus(menuVoList, menuid);

                    Tree t = new Tree();
                    t.setMid(menu.getMid());
                    t.setLabel(menu.getMname());
                    t.setPid(menu.getP_mid());
                    t.setHerf(menu.getPath());
                    t.setChildren(iterateMenu);
                    result.add(t);
                }
            }
        }
        return result;
    }


}
